import {createRouter, createWebHashHistory} from "vue-router"
import Layout from "@/layout";
import userLayout from '@/views/profile/user'
import {getRouteName} from "@/utils/access";
//静态路由不需要权限校验，可以直接访问
export const constantRoutes=[
  //首页
  {
    path:'/',
    component:Layout,
    redirect:'/homepage',
    children:[{
      path:'homepage',
      name:'homepage',
      component:() => import('@/views/homepage'),
      meta:{path:'/homepage',title:'首页',roles:['user','admin']},
    },
    ]
  },
  {
    path:'/article',
    name:'article',
    component: Layout,
    meta: {title: '最新动态'},
    children: [
      {
        path: 'news',
        name:'news',
        component: () => import('@/views/article/scenicNews'),
        meta:{path:'/article/news',title:'景区新闻'}
      },
      {
        path: 'announce',
        name:'announce',
        component: () => import('@/views/article/scenicAnnounce'),
        meta:{path:'/article/announce',title:'景区公告'}
      }
    ]
  },
  //登录页
  {
    path:'/login',
    name:'login',
    component:() => import('@/views/loginAndRegister'),
    meta:{path:'/login',title:'登录'},
    props({query}){
      return{queryPath:query.redirect}
    }
  },
  /*{
    path: '/helpCenter',
    name: 'help',
    component:Layout,
    meta:{title:'帮助中心'},
    children: [{
      path: 'help',
      name: 'help',
      component:() => import('@/views/help'),
      meta:{path:'/helpCenter/help',title:'帮助中心',roles:[]}
    }]
  },*/
  {
    path: '/scenic',
    name: 'scenic',
    component:Layout,
    meta:{title:'景点'},
    children: [{
      path: 'detail',
      name: 'detail',
      component:() => import('@/views/scenic'),
      meta:{path:'/scenic/detail',title:'景点浏览',roles:[]}
    }]
  },
  {
    path: '/service',
    name: 'service',
    component:Layout,
    meta:{title:'service'},
    children: [{
      path: 'hotel',
      name: 'hotel',
      component:() => import('@/views/smartService/hotel'),
      meta:{path:'/service/hotel',title:'住宿推荐',roles:[]}
    }]
  },
  {
    path: '/403',
    name:'403',
    component: () => import('@/views/noPermission'),
    meta:{path:'/403',title:'403',hidden:true}
  },
  {
    path: '/404',
    name:'404',
    component: () => import('@/views/errorPage'),
    meta:{path:'/404',title:'404',hidden:true}
  },
  {
    path: '/warning',
    name:'warning',
    component: () => import('@/views/waningPage'),
    meta:{path:'/warning',title:'warning',hidden:true}
  },
  {
    path:'/showArticle/:articleId',
    component: () => import('@/views/article/showArticle'),
    meta:{path:'/showArticle',title:'文章详情',hidden:true}
  }
]

//异步路由需要通过用户角色动态生成
export const asyncRoutes=[
//用户路由
  {
    path:'/smart',
    name:'smart',
    component:Layout,
    meta: {title:'智慧服务',roles: ['user']},
    children:[

      {
        path: 'buyTicket/:ticketId',
        name: 'buyTicket',
        props: true,
        component:() => import('@/views/smartService/ticket/buyTicket'),
        meta:{path:'/smart/buyTicket',title:'购票页',roles:['user']}
      },
      {
        path: 'getTicket',
        name: 'getTicket',
        component:() => import('@/views/smartService/ticket'),
        meta:{path:'/smart/getTicket',title:'票务详情',roles:['user']}
      },
    ]
  },
  //管理员路由
  {
    path:'/articleAdmin',
    name:'articleAdmin',
    component:Layout,
    meta:{title:'文章',roles:['admin']},
    children: [
      {
        path:'publishArticle',
        name:'publishArticle',
        component:() => import('@/views/article/publish'),
        meta:{path:'/articleAdmin/publishArticle',title:'文章发布',roles:['admin']}
      },
      {
        path:'articleList',
        name:'articleList',
        component:() => import('@/views/article/articleListAdmin'),
        meta:{path:'/articleAdmin/articleList',title:'文章列表',roles:['admin']}
      },
      {
        path:'recycleBin',
        name:'recycleBin',
        component:() => import('@/views/article/articleListAdmin/recycleBin'),
        meta:{path:'/articleAdmin/recycleBin',title:'文章回收站',roles:['admin']}
      },
      {
        path:'articleDetail/:articleId',
        name:'articleDetail',
        component:() => import('@/views/article/articleListAdmin/showArticleDeleted'),
        meta:{path:'/articleAdmin/articleDetail',title:'文章详情',roles:['admin'],hidden:true}
      },
    ]
  },
  {
    path:'/scenicOperate',
    name:'scenicOperate',
    component: Layout,
    meta:{title: '景点',roles:['admin']},
    children: [
      {
        path:'insertScenic',
        name:'insertScenic',
        component:()=> import('@/views/scenic/scenicOperation/insertScenic'),
        meta:{path:'/scenicOperate/insertScenic',title: '景点录入',roles:['admin']}
      },
      {
        path:'lookupScenic',
        name:'lookupScenic',
        component:()=> import('@/views/scenic/scenicOperation/lookupScenic'),
        meta:{path:'/scenicOperate/lookupScenic',title: '景点列表',roles:['admin']}
      },
      {
        path:'recycleScenic',
        name:'recycleScenic',
        component:()=> import('@/views/scenic/recycle'),
        meta:{path:'/scenicOperate/recycleScenic',title: '景点回收站',roles:['admin']}
      },
      {
        path:'updateScenic/:scenicId',
        name:'updateScenic',
        props:true,
        component:()=> import('@/views/scenic/scenicOperation/scenicInsertOrUpdate/updateScenic'),
        meta:{path:'/scenicOperate/updateScenic',title: '景点回收站',roles:['admin'],hidden: true}
      }
    ]
  },
  {
    path:'/ticket',
    name:'ticket',
    component: Layout,
    meta: {title:'票务',roles: ['admin']},
    children: [
      {
        path: 'publish',
        name:'publish',
        component: () => import('@/views/smartService/ticket/ticketAdmin/publish'),
        meta:{path:'/ticket/publish',title:'门票发布',roles: ['admin']}
      },
      {
        path: 'lookTicket',
        name:'lookTicket',
        component: () => import('@/views/smartService/ticket/ticketAdmin/lookTicket'),
        meta:{path:'/ticket/lookTicket',title:'门票查看',roles: ['admin']}
      },
      {
        path: 'searchTicket',
        name:'searchTicket',
        component: () => import('@/views/smartService/ticket/searchTicket'),
        meta:{path:'/ticket/searchTicket',title:'用户购票查询',roles: ['admin']}
      },
      {
        path: 'scanTicket',
        name:'scanTicket',
        component: () => import('@/views/smartService/ticket/ticketAdmin/scanCode'),
        meta:{path:'/ticket/scanTicket',title:'检票',roles: ['admin']}
      }
    ]
  },

  {
    path:'/circum',
    name: 'circum',
    component: Layout,
    meta:{title:'周边',roles:['admin']},
    children:[
      {
        path:'createHotel',
        name: 'createHotel',
        component:()=> import('@/views/circum/hotel/adminHotel/create'),
        meta:{path:'/circum/createHotel',title:'添加旅馆',roles:['admin']},
      },
      {
        path:'hotelList',
        name: 'hotelList',
        component:()=> import('@/views/circum/hotel/adminHotel/hotelList'),
        meta:{path:'/circum/hotelList',title:'旅馆列表',roles:['admin']},
      },
      {
        path:'editHotel/:hotelId',
        name: 'editHotel',
        props: true,
        component:()=> import('@/views/circum/hotel/adminHotel/edit'),
        meta:{path:'/circum/editHotel',title:'编辑旅馆信息',roles:['admin'], hidden: true},
      }
    ]
  },
  {
    path: '/scanCode',
    name:'scanCode',
    component: () => import('@/views/smartService/ticket/ticketAdmin/scanCode'),
    meta:{path:'/scanCode',title:'检票',roles: ['admin'],hidden:true}
  },
  {
    path:'/fallback',
    name: 'fallback',
    component: Layout,
    meta:{title:'用户反馈',roles:['admin']},
    children:[
      {
        path:'note',
        name: 'note',
        component:()=> import('@/views/fallback'),
        meta:{path:'/fallback/note',title:'用户反馈',roles:['admin']},
      }
    ]
  },
  {
    path:'/manageAccount',
    name: 'manageAccount',
    component: Layout,
    meta:{title:'用户管理',roles:['admin']},
    children:[
      {
        path:'userList',
        name: 'userList',
        component:()=> import('@/views/userManage'),
        meta:{path:'/manageAccount/userList',title:'用户管理',roles:['admin']},
      }
    ]
  },
  {
    path:'/realAnnounce',
    name: 'realAnnounce',
    component: Layout,
    meta:{title:'实时公告',roles:['admin']},
    children:[
      {
        path:'publishReal',
        name: 'publishReal',
        component:()=> import('@/views/realAnnounce'),
        meta:{path:'/realAnnounce/publishReal',title:'实时公告',roles:['admin']},
      }
    ]
  },
  //个人中心
  {
    path:'/profile',
    name:'profile',
    component: Layout,
    meta: {title:'用户',roles: ['user']},
    children: [
      {
        path: 'user',
        name:'user',
        component: userLayout,
        meta:{title:'个人中心',roles: ['user']},
        children:[
          {
            path: 'userInfo',
            name:'userInfo',
            component: () => import('@/views/profile/user/userInfo'),
            meta:{path:'/profile/user/userInfo',title:'个人资料',roles: ['user']}
          },
          {
            path: 'userTicket',
            name:'userTicket',
            component: () => import('@/views/profile/user/userTicket'),
            meta:{path:'/profile/user/userTicket',title:'我的门票',roles: ['user']}
          },
          {
            path: 'ticketDetail/:ticketId',
            name:'ticketDetail',
            props:true,
            component: () => import('@/views/profile/user/userTicket/ticketDetail'),
            meta:{path:'/profile/user/ticketDetail',title:'门票详情',roles: ['user']}
          },
        ]
      },
    ]
  },
  {
    path: '/admin',
    name:'admin',
    redirect:'/admin/index',
    component: Layout,
    meta:{title:'个人中心',roles: ['admin']},
    children: [
      {
        path: 'index',
        name:'index',
        component:() => import('@/views/profile/admin') ,
        meta:{path:'/admin/index',title:'账户信息',roles: ['admin']},
      },
    ]
  },

  /*{
    path:'/NoRoute',
    component: Layout,
    meta:{title:'退出',roles:['admin']},
    children: [{
      path: 'logout',
      name:'logout',
      meta: {path:'/NoRoute/logout',title: '退出登录',roles: ['admin']}
    }]
  }*/

]


// 导出静态路由
const router=createRouter({
  routes:constantRoutes,
  history:createWebHashHistory()
})
export function resetRouter(role){
  const routes=getRouteName(role,asyncRoutes)
  router.getRoutes().forEach(route=>{
    if (routes.indexOf(route.name)!==-1){
      router.removeRoute(route.name)
    }
  })
}
export default router

